//
//  ModeTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/20.
//

import UIKit
protocol ModeTableViewCellDelegate{
    func modeTableViewCelldidSelect(index: NSInteger)
}


class ModeTableViewCell: UITableViewCell {
    var collectionView: UICollectionView!
    var selectIndex = 0
    var delegate: ModeTableViewCellDelegate!
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?){
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = CGSize(width: kScreenWidth/2, height: 45)
        layout.scrollDirection = .horizontal
        
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: self.frame.size.height), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.backgroundColor = .white
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.shouldHideToolbarPlaceholder = false
        self.collectionView.register(ModeCollectionViewCell.self, forCellWithReuseIdentifier: "ModeCollectionViewCell")
        self.contentView.addSubview(self.collectionView)
    }
    
    
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}

extension ModeTableViewCell: UICollectionViewDelegate,UICollectionViewDataSource{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return 2
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ModeCollectionViewCell", for: indexPath) as! ModeCollectionViewCell
        
        switch indexPath.row {
        case 0:
            cell.titleLab.text = MMLocalizedString("lease_contribution", comment: "租赁供款")
        default:
            cell.titleLab.text = MMLocalizedString("rent_purchase_contributions", comment: "租购供款")
        }
        
        if self.selectIndex == indexPath.row{
            cell.titleLab.textColor = Theme.mainColor
            cell.imageView.isHidden = false
        }else{
            cell.titleLab.textColor = Theme.titleLabColor
            cell.imageView.isHidden = true
        }
        cell.setIndex(index: indexPath.row)
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        self.selectIndex = indexPath.row
        self.collectionView.reloadData()
        self.delegate?.modeTableViewCelldidSelect(index: indexPath.row)
    }
    
}


class ModeCollectionViewCell: UICollectionViewCell {
    var titleLab: UILabel!
    var imageView: UIImageView!
    var questionBtn: UIButton!
    var index: NSInteger!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        self.clipsToBounds = true
        
        
        
        self.titleLab = UILabel(frame: .zero)
        self.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 14)
        self.titleLab.textColor = Theme.mainColor
        self.titleLab.textAlignment = .center
        self.contentView.addSubview(self.titleLab)
        
        self.questionBtn = UIButton(frame: CGRect(x: 0, y: 0, width: 14, height: 14))
        self.questionBtn.setImage(UIImage(named: "icon_question_mark"), for: .normal)
        self.questionBtn.addTarget(self, action: #selector(questionBtnAntion), for: .touchUpInside)
        self.questionBtn.contentHorizontalAlignment = .left
        self.contentView.addSubview(self.questionBtn)
        
        
        self.imageView = UIImageView(frame: .zero)
        self.imageView.image = UIImage(named: "icon_subfield")
        self.contentView.addSubview(self.imageView)
        
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(7)
            make.centerX.equalTo(self.contentView.snp.centerX)
            make.height.equalTo(30)
        }
        
        self.questionBtn.snp.makeConstraints { make in
            make.top.equalTo(7)
            make.left.equalTo(self.titleLab.snp.right).offset(4)
            make.width.equalTo(30)
            make.height.equalTo(30)
        }
        
        self.imageView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom)
            make.width.equalTo(21)
            make.centerX.equalTo(self.titleLab.snp.centerX)
            make.height.equalTo(6)
        }
        
    }
    
    
    //setModel
    func setIndex(index: NSInteger){
        self.index = index
    }
    
    
    
    @objc func questionBtnAntion(){
        
        var type: WBArrowDirection = .up1
        var installment =  MMLocalizedString("contribution_explain", comment: "无需首期，只需预先支付固定期数的供款，可缩短供款期限，并享受更低的优惠利率。")
        if self.index == 1{
            installment = MMLocalizedString("car_price_installments", comment: "以固定月供及优惠利率分期缴付车价。")
            type = .up3
        }
        
        let contentLab = UILabel(frame: .init(x: 12, y: 12, width: 250, height: 1000))
        contentLab.font = UIFont.init(name: Theme.PingFangSCRegular, size: 12)
        contentLab.textColor = .white
        contentLab.numberOfLines = 0
        contentLab.text = installment
        contentLab.sizeToFit()
        let tempH: CGFloat = contentLab.frame.size.height
       
        let parantView = self.viewController().view
        guard let toPoint = parantView?.convert(self.questionBtn.center, fromViewOrWindow: self) else { return  }
        let overView = WBPopOverView.init(origin: toPoint, width:274, height: Float(tempH) + 24, direction: type)
        overView?.backView.backgroundColor = UIColor(hex: 0x222222)
        overView?.backView.layer.cornerRadius = 8
        overView?.backView.layer.masksToBounds = true
        overView?.backView.addSubview(contentLab)
        overView?.pop()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
